package DoublePointer;

import org.junit.Test;

import java.util.Arrays;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: DoublePointer
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-11-03  15:59
 * @Description: TODO
 * @Version: 1.0
 */
public class 移除数组中的元素27 {

    public int removeElement(int[] nums, int val) {
        int left = 0;
        int right = nums.length;
        while (left < right) {
            if (nums[left] == val) {
                nums[left] = nums[right - 1];
                right--;
            } else {
                left++;
            }
        }
        return left;
    }

    public int removeElement2(int[] nums, int val) {
       int left=0;
       int right=nums.length-1;
       while (left<right){
           if (nums[left]==val){
               nums[left]=nums[right-1];
               right--;
           }else {
               left++;
           }
       }
       return left;
    }

    public int removeElement3(int[] nums, int val) {
        int fast=0;
        int slow=0;
        while (fast<=nums.length-1){
            if (nums[fast]!=val){
                nums[slow]=nums[fast];
                slow++;
                fast++;
            }else {
                fast++;
            }
        }
        return slow;
    }

    @Test
    public void test(){
        int i = removeElement3(new int[]{0,1,2,2,3,0,4,2}, 2);
        System.out.println(i);
    }

}
